<template>
<el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false" width="50%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '170px' : '140px'">
        <el-row :gutter="30">
            <el-col :span="12">
                <el-form-item label="项目名称" prop="projectName">
                    <el-input v-model="dataForm.projectName"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="分类" prop="categoryId">
                    <el-select v-model="dataForm.categoryId" placeholder="请选择" clearable style="width:100%">
                        <el-option v-for="(item, index) in classfyList" :label="item.categoryName" :key="index" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="单位" prop="unit">
                    <el-input v-model="dataForm.unit"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="默认价格" prop="defaultPrice">
                    <el-input v-model="dataForm.defaultPrice" type="number" min="0"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
    <template slot="footer">
        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
</el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
    data() {
        return {
            visible: false,
            classfyList: [],
            dataForm2: {
                page: 1,
                limit: 100,
                homestayId: this.$store.state.user.homestayId,
            },
            dataForm: {
                id: "",
                homestayId: this.$store.state.user.homestayId,
                categoryName: "",
                defaultPrice: 0,
                projectName: "",
                categoryId: '',
                unit: "",
            },
        };
    },
    computed: {
        dataRule() {
            return {
                projectName: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
                categoryName: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
                defaultPrice: [{
                    required: true,
                    message: this.$t("validate.required"),
                    trigger: "blur",
                }, ],
            };
        },
    },
    methods: {
        init() {
            this.visible = true;
            this.getClassfy();
            this.$nextTick(() => {
                this.$refs["dataForm"].resetFields();
                if (this.dataForm.id) {
                    this.getInfo();
                }
            });
        },
        //获取分类
        getClassfy() {
            this.$http
                .get("/hotel/other/expense/category/page", {
                    params: this.dataForm2
                })
                .then((res) => {
                    if (res.data.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    this.classfyList = res.data.data.list;
                });
        },
        // 获取信息
        getInfo() {
            this.$http
                .get("/hotel/other/expense/project/" + this.dataForm.id)
                .then(({
                    data: res
                }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    this.dataForm = {
                        ...this.dataForm,
                        ...res.data,
                    };
                })
                .catch(() => {});
        },
        // 表单提交
        dataFormSubmitHandle: debounce(
            function () {
                this.$refs["dataForm"].validate((valid) => {
                    if (!valid) {
                        return false;
                    }

                    this.$http[!this.dataForm.id ? "post" : "put"](
                            "/hotel/other/expense/project",
                            this.dataForm
                        )
                        .then(({
                            data: res
                        }) => {
                            if (res.code !== 0) {
                                return this.$message.error(res.msg);
                            }
                            this.$message({
                                message: this.$t("prompt.success"),
                                type: "success",
                                duration: 500,
                                onClose: () => {
                                    this.visible = false;
                                    this.$emit("refreshDataList");
                                },
                            });
                        })
                        .catch(() => {});
                });
            },
            1000, {
                leading: true,
                trailing: false
            }
        ),
    },
};
</script>

<style scoped lang="scss">
h3 {
    padding: 30px 0 15px;
    width: 94%;
    margin: 0 auto 30px;
    border-bottom: 1px solid #ddd;
    color: #303133;
    font-size: 22px;
    overflow: hidden;
}

.rooms .item {
    display: inline-block;
    margin-right: 15px;
    position: relative;

    .el-icon-error {
        position: absolute;
        right: 6px;
        top: -6px;
        font-size: 20px;
        color: #dd0606;
    }
}
</style>
